

const {ccclass, property} = cc._decorator;

declare global {
    interface String {
        format(...args: any[]): string;
    }
}


String.prototype.format = function (...args) {
    
    let result = <string>this;
    if (args.length > 0) {
        if (args.length == 1 && typeof args[0] == 'object') {
            for (let key in args) {
                if (args[key] != undefined) {
                    let reg = new RegExp('({' + key + '})', 'g');
                    result = result.replace(reg, args[key]);
                }
            }
        }
        else {
            for (let i = 0; i < args.length; i++) {
                if (args[i] != undefined) {
                    let reg = new RegExp('({)' + i + '(})', 'g');
                    result = result.replace(reg, args[i]);
                }
            }
        }
    }
    return result;
};

@ccclass
export default class NewClass extends cc.Component {

    mima: string[];
    mimaIndex = 0;


    onLoad() {
        
        if (!cc.sys.isNative && cc.sys.isMobile) {
            let cvs = this.node.getComponent(cc.Canvas);
            cvs.fitWidth = true;
            cvs.fitHeight = true;
        }

        if(!cc.vv){
            cc.director.loadScene('loading');
            return;
        }

        cc.vv.http.url = cc.vv.http.master_url;
        //监听创建角色事件
        // cc.vv.net.addHandler('push_need_create_role', function(){
        //     cc.director.loadScene('createrole');
        // });
        //播放背景音乐
        cc.vv.audioMgr.playBGM('bgMain.mp3');

        this.mima = ['A', 'A', 'B', 'B', 'A', 'B', 'A', 'B', 'A', 'A', 'A', 'B', 'B', 'B'];

        if(!cc.sys.isNative){
            //web页面显示游客登陆按钮
            cc.find('Canvas/btn_yk').active = true;
            cc.find('Canvas/btn_weixin').active = false;
        }
        else{
            //手机上显示微信登陆按钮
            cc.find('Canvas/btn_yk').active = false;
            cc.find('Canvas/btn_weixin').active = true;
        }
    }

    start(){
        const account = cc.sys.localStorage.getItem('wx_account');
        const sign = cc.sys.localStorage.getItem('wx_sign');
        if(account != null && sign != null){
            const ret = {
                errcode: 0,
                account: account,
                sign: sign
            };
            cc.vv.userMgr.onAuth(ret);
        }
        
    }

    /**
     * 游客登陆
     */
    onBtnQuickStartChilked(){
        cc.vv.userMgr.guestAuth();
    }

    /**
     * 微信登陆
     */
    onBtnWeichatClicked(){
        console.log('点击了微信登录');
        cc.vv.anysdk.login();
    }


    /**
     * 彩蛋登陆
     * 原生应用显示游客登陆、
     * 左左右右左右左右左左左右右右
     */
    onBtnMIMAClicked(event:cc.Event.EventTouch){
        if(this.mima[this.mimaIndex] == event.target.name){
            this.mimaIndex++;
            if(this.mimaIndex == this.mima.length){
                cc.find('Canvas/btn_yk').active = true;
            }
        }
        else{
            console.log('失败了，再来一次吧。');
            this.mimaIndex = 0;
        }
    }
}
